﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;
using BTN04.BUS;
using BTN04.DTO;

namespace BTN04.ADMIN
{
    public partial class quanlysanpham : System.Web.UI.Page
    {
        int index;
        protected void Page_Load(object sender, EventArgs e)
        {

            //Hien thi danh sach nha san xuat len droplist
            List<NhaSanXuatDTO> ds_nsx = new List<NhaSanXuatDTO>();
            NhaSanXuatBUS nsxBus = new NhaSanXuatBUS();
            ds_nsx = nsxBus.ThongTinToanBoNhaNhaSanXuat();

            //Hien thi danh sach loai san pham len droplist
            List<LoaiSanPhamDTO> ds = new List<LoaiSanPhamDTO>();
            LoaiSanPhamBUS LspBus = new LoaiSanPhamBUS();
            ds = LspBus.DanhSachLoaiSanPham();

            //Hien thi danh sach xuat xu len droplist
            List<XuatSuDTO> ds_xuatsu = new List<XuatSuDTO>();
            XuatSuBUS xsBus = new XuatSuBUS();
            ds_xuatsu = xsBus.DanhSach();

            //Hient thi danh sach Tinh Trang Loai San Pham len dropList
            DataTable dt3 = new DataTable();
            TinhTrangSanPhamBUS trspBus = new TinhTrangSanPhamBUS();
            dt3 = trspBus.LayDanhSachTinhTrangSanPham();

            //Hien thi danh sach khuyen mai len dropList
            DataTable dt4 = new DataTable();
            KhuyenMaiBUS kmBus = new KhuyenMaiBUS();
            dt4 = kmBus.LayDanhSachKhuyenMai();

            List<SanPhamDTO> ds1 = new List<SanPhamDTO>();
            SanPhamBUS sp = new SanPhamBUS();
            ds1 = sp.HienThiChiTietToanBoSanPham();

            if (!IsPostBack)
            {
                for (int i = 0; i < ds_nsx.Count - 1; i++)
                {
                    ListItem lv = new ListItem();
                    lv.Value = ds_nsx[i].MaNhaSanXuat.ToString();
                    lv.Text = ds_nsx[i].TenNhaSanXuat.ToString();
                    drNhaSX.Items.Add(lv);
                }
                for (int i = 0; i < ds.Count; i++)
                {
                    ListItem lv = new ListItem();
                    lv.Value = ds[i].MaLoaiSanPham.ToString();
                    lv.Text = ds[i].TenLoaiSanPham.ToString();
                    drLoaiSP.Items.Add(lv);
                }
                for (int i = 0; i < ds_xuatsu.Count - 1; i++)
                {
                    ListItem lv = new ListItem();
                    lv.Value = ds_xuatsu[i].MaXuatSu.ToString();
                    lv.Text = ds_xuatsu[i].TenXuatSu.ToString();
                    drXuatXu.Items.Add(lv);
                }
                for (int i = 0; i < dt3.Rows.Count; i++)
                {
                    ListItem lv = new ListItem();
                    lv.Value = dt3.Rows[i]["MaTinhTrangSanPham"].ToString();
                    lv.Text = dt3.Rows[i]["TenTinhTrangSanPham"].ToString();
                    drTinhTrangSP.Items.Add(lv);


                }
                for (int i = 0; i < dt4.Rows.Count; i++)
                {
                    ListItem lv = new ListItem();
                    lv.Value = dt4.Rows[i]["MaKhuyenMai"].ToString();
                    lv.Text = dt4.Rows[i]["TenKhuyenMai"].ToString();
                    drKhuyenMai.Items.Add(lv);
                }
            }

        }


        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ThongBao.Text = "";
            SanPhamBUS spBus = new SanPhamBUS();
            index = GridView1.SelectedIndex;
            GridViewRow row = GridView1.SelectedRow;

            if (index >= 0)
            {
                int ma = int.Parse(row.Cells[0].Text);
                SanPhamDTO spDto = new SanPhamDTO();
                spDto = spBus.HienThiChiTietSanPhamTheoMa(ma);
                txtMaSP.Text = spDto.MaSanPham.ToString();
                txtTenSP.Text = spDto.TenSanPham.ToString();

                drLoaiSP.Items[drLoaiSP.SelectedIndex].Text = spDto.TenLoaiSanPham;
                drTinhTrangSP.Items[drTinhTrangSP.SelectedIndex].Text = spDto.TenTinhTrangSanPham.ToString();
                drNhaSX.SelectedItem.Text = spDto.TenNhaSanXuat.ToString();

                drLoaiSP.Text = spDto.MaLoaiSanPham.ToString();
                drTinhTrangSP.Text = spDto.MaTinhTrangSanPham.ToString();
                drNhaSX.Text = spDto.MaNhaSanXuat.ToString();

                HinhSP.ImageUrl = "~/" + txtHinhAnh.Text;
                txtGiaBan.Text = spDto.GiaBan.ToString();



                txtSoLuongXem.Text = spDto.SoLuongXem.ToString();
                drXuatXu.Text = spDto.MaXuatSu.ToString();
                txtHinhAnh.Text = spDto.Hinh.ToString();
                drKhuyenMai.Text = spDto.MaKhuyenMai.ToString();
                txtMoTa.Text = spDto.MoTa.ToString();
            }



        }
        //xu ly khi nhan nut sua

        protected void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaSP.Text != "")
            {
                SanPhamDTO spDto = new SanPhamDTO();
                spDto.MaSanPham = int.Parse(txtMaSP.Text);
                spDto.TenSanPham = txtTenSP.Text;
                spDto.MaLoaiSanPham = int.Parse(drLoaiSP.Items[drLoaiSP.SelectedIndex].Value.ToString());
                spDto.MaTinhTrangSanPham = int.Parse(drTinhTrangSP.Items[drTinhTrangSP.SelectedIndex].Value.ToString());
                spDto.MaNhaSanXuat = int.Parse(drNhaSX.Items[drNhaSX.SelectedIndex].Value.ToString());
                spDto.GiaBan = int.Parse(txtGiaBan.Text);
                spDto.SoLuongXem = int.Parse(txtSoLuongXem.Text);
                spDto.MaXuatSu = int.Parse(drXuatXu.Items[drXuatXu.SelectedIndex].Value.ToString());
                spDto.Hinh = txtHinhAnh.Text;
                spDto.MoTa = txtMoTa.Text;
                spDto.MaKhuyenMai = int.Parse(drKhuyenMai.Items[drKhuyenMai.SelectedIndex].Value.ToString());



                SanPhamBUS spBus = new SanPhamBUS();
                int kq = spBus.CapNhatMotSanPham(spDto);
                if (kq > 0)
                {
                    ThongBao.Text = "Cập nhật thành công";
                }
                else
                {
                    ThongBao.Text = "Cập nhật thất bại";

                }
                Response.Redirect("~/ADMIN/quanlysanpham.aspx");

            }
            else
            {
                ThongBao.Text = "Chưa chọn sản phẩm cần xóa";
            }





        }

        protected void btnThem_Click(object sender, EventArgs e)
        {

            SanPhamDTO spDto = new SanPhamDTO();

            spDto.TenSanPham = txtTenSP.Text;
            spDto.MaLoaiSanPham = int.Parse(drLoaiSP.Items[drLoaiSP.SelectedIndex].Value.ToString());
            spDto.MaTinhTrangSanPham = int.Parse(drTinhTrangSP.Items[drTinhTrangSP.SelectedIndex].Value.ToString());
            spDto.MaNhaSanXuat = int.Parse(drNhaSX.Items[drNhaSX.SelectedIndex].Value);
            spDto.GiaBan = int.Parse(txtGiaBan.Text);
            spDto.SoLuongXem = int.Parse(txtSoLuongXem.Text);
            spDto.MaXuatSu = int.Parse(drXuatXu.Items[drXuatXu.SelectedIndex].Value.ToString());
            spDto.Hinh = txtHinhAnh.Text;
            spDto.MoTa = txtMoTa.Text;
            spDto.MaKhuyenMai = int.Parse(drKhuyenMai.Items[drKhuyenMai.SelectedIndex].Value.ToString());



            SanPhamBUS spBus = new SanPhamBUS();
            int kq = spBus.ThemMotSanPham(spDto);
            if (kq > 0)
            {
                ThongBao.Text = "Thêm thành công";
            }
            else
            {
                ThongBao.Text = "Thêm thất bại";

            }

            Response.Redirect("~/ADMIN/quanlysanpham.aspx");
        }

        protected void btnXoa_Click(object sender, EventArgs e)
        {

            if (txtMaSP.Text != "")
            {
                int ma = int.Parse(txtMaSP.Text);
                SanPhamBUS spBus = new SanPhamBUS();
                int kq = spBus.XoaMotSanPhamDuaVaoMa(ma);
                if (kq > 0)
                {
                    ThongBao.Text = "Xóa Thành Công";
                }

                else
                {

                    ThongBao.Text = "Xóa Thất Bại";

                    ThongBao.Text = "Xóa Thất Bại";


                }

            }
            else
            {
                ThongBao.Text = "Chưa chọn sản phẩm cần sửa";
            }
            Response.Redirect("~/ADMIN/quanlysanpham.aspx");



        }

        protected void btnThemMoi_Click(object sender, EventArgs e)
        {
            txtTenSP.Text = "";
            txtMaSP.Text = "";
            txtSoLuongXem.Text = "";
            txtGiaBan.Text = "";
            txtHinhAnh.Text = "";
            txtMoTa.Text = "";

        }
        //UpLoad Hinh thay the

        protected void btnUpLoad_Click(object sender, EventArgs e)
        {
            HttpPostedFile hinh = UpLoaiHinh.PostedFile;


            if (!File.Exists(Server.MapPath("~\\Images\\UpLoad") + "\\" + Path.GetFileName(hinh.FileName)))
            {
                hinh.SaveAs(Server.MapPath("~\\Images\\UpLoad") + "\\" + Path.GetFileName(hinh.FileName));
                txtHinhAnh.Text = "Images" + "\\" + "UpLoad" + "\\" + Path.GetFileName(hinh.FileName);
            }
        }
    }
}

